/*
 * #%L
 * java model
 * %%
 * Copyright (C) 2009 - 2011 prefabware.com Stefan Isele
 * %%
 * All Rights Reserved.
 * This program is dual-licensed under the GNU General Public license
 * or a commercial license.
 * You may select to use one or the other of these licenses.
 * To obtain a commercial license please contact Stefan Isele at www.stefan-isele.de
 * Any modifications to this file must keep this entire header intact.
 * Redistribution, except as permitted by the selected license, is prohibited.
 * #L%
 */
package com.prefabware.jmodel;

import com.prefabware.commons.Namespace;
import com.prefabware.commons.NamespaceImpl;
import com.prefabware.jmodel.code.JCodable;
import com.prefabware.jmodel.code.JCodeOptions;

public class JImport implements JCodable {
	public static class JGenericImport extends JImport {

		public JGenericImport(Namespace namespace) {
			super(namespace);
		}
	}

	private Namespace namespace;

	/**
	 * to create a JImport for the class
	 * 
	 * @param clazz
	 */
	public static JImport create(Class<?> clazz) {
		return create(clazz.getName());
	}

	/**
	 * to create a JImport for the type
	 * 
	 * @param type
	 */
	public static JImport create(JType type) {
		return create(type.getQualifiedName().getName());
	}

	
	/**
	 * to create a JImport for the namespace
	 * 
	 * @param namespace
	 *            a String consisting of segments seperated by a dot '.' <br>
	 */
	private JImport(Namespace ns) {
		this.namespace=ns;
	}

	public static JImport create(String import_) {
		Namespace ns = new NamespaceImpl(import_, ".");
		if (ns.lastElement().equals("*")) {
			return new JGenericImport(ns);
		}
		return new JImport(ns);
	};

	@Override
	public String asCode(JCodeOptions options) {
		return this.namespace.packageStyle();
	}

}
